package com.example.demo.controller;
import com.example.demo.Dao.DaoFactory;
import com.example.demo.Dao.GoodsDaoImpl;
import com.example.demo.bean.Goods;
import com.example.demo.service.GoodsDao;
import com.example.demo.controller.LoginController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@CrossOrigin
public class GoodsController {
    @Autowired
  //  private JdbcTemplate jdbcTemplate;
    GoodsDao dao = DaoFactory.getGoodsDao();
   LoginController login=new LoginController();
   boolean a= login.a;

    @RequestMapping("/getGoods")
    public String getDbType(Model m){
        List<Goods> list=dao.findGoodsList();
        m.addAttribute("goods",list);
        return "goods";
   /*     String sql1 = "select * from bread";
        List<Map<String, Object>> list1 =  jdbcTemplate.queryForList(sql1);
        for (Map<String, Object> map : list1) {
            Set<Map.Entry<String, Object>> entries = map.entrySet();// 通过entrySet()方法将map集合中的映射关系(key-value)取出（这个关系就是Map.Entry类型）
            if (entries != null) {
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();// 将关系集合entryset进行迭代，存放到迭代器中
                while (iterator.hasNext()) {
                    Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();// 获取Map.Entry关系对象
                    Object key = entry.getKey();
                    Object value = entry.getValue();

                  /*  if("name".equals(key)&&"zhangsan".equals(value))
                    {System.out.println(123456789);
                    }

                    System.out.println(key + ":" + value);
                    if("price".equals(key))
                    {System.out.println(); }
                }
            }
        }
        m.addAttribute("goods",list1);
        return "goods";*/
    }


    @RequestMapping("/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response ,Model m) throws Exception {
        long id_delete = Integer.parseInt(request.getParameter("id"));
        System.out.println(id_delete);
        dao.del(id_delete);
        List<Goods> list1=dao.findGoodsList();
        m.addAttribute("goods",list1);
        return "goods";
    }

    @RequestMapping("/updatejsp")
    public String update_jsp(HttpServletRequest request, HttpServletResponse response ,Model m)  {
        long id = Integer.parseInt(request.getParameter("id"));
        System.out.println(id);
        List<Goods> list= dao.findGoodsById(id);
        m.addAttribute("goods",list);
        return "update";
    }


    @RequestMapping("/update")
    public String update(HttpServletRequest request, HttpServletResponse response ,Model m) throws Exception {
        long id_update = Integer.parseInt(request.getParameter("id"));
        String Name_update=request.getParameter("name");
        int amount_update=Integer.parseInt(request.getParameter("amount"));
        double price_update=Double.parseDouble(request.getParameter("price"));
        // System.out.println(id);
        Goods goods = new Goods();
        goods.setId(id_update);
        goods.setName(Name_update);
        goods.setAmount(amount_update);
        goods.setPrice(price_update);
//         boolean boo=dao.checkId(id_update);
//        if(boo==false){
//           dao.add(goods);
//        }
//        else{
//           dao.update(goods);
//        }
        dao.update(goods);
        List<Goods> list1=dao.findGoodsList();
        m.addAttribute("goods",list1);
        return "goods";
    }

    @RequestMapping("/search")
    public String search(HttpServletRequest request, HttpServletResponse response ,Model m) throws Exception {
        String searchText = request.getParameter("searchText");
        boolean isDigit = false;
        for(int i=0 ; i<searchText.length() ; i++) { //循环遍历字符串
            if (Character.isDigit(searchText.charAt(i))) {     //用char包装类中的判断数字的方法判断每一个字符
                isDigit = true;
            }
        }
        if(isDigit){
            long id = Integer.parseInt(searchText);
            List<Goods> list1=dao.findGoodsById(id);
            m.addAttribute("goods",list1);
        }
        else{
            List<Goods> list1=dao.findGoodsByName(searchText);
            m.addAttribute("goods",list1);
        }
        return "goods";
    }

    @RequestMapping("/insertjsp")
    public String update_jsp()  {
        return "insert";
    }

    @RequestMapping("/insert")
    public String insert(HttpServletRequest request, HttpServletResponse response ,Model m) throws Exception {
        String Name_insert=request.getParameter("name");
        int amount_insert=Integer.parseInt(request.getParameter("amount"));
        double price_insert=Double.parseDouble(request.getParameter("price"));
        // System.out.println(id);
        Goods goods = new Goods();
        goods.setName(Name_insert);
        goods.setAmount(amount_insert);
        goods.setPrice(price_insert);
        dao.add(goods);
        List<Goods> list1=dao.findGoodsList();
        m.addAttribute("goods",list1);
        return "goods";
    }




}